<?php
namespace app;
use think\Request;
use think\Validate;
use think\Db;
use think\Controller;

class basecontroller extends Controller {
    public function ErrorSend($error)
    {
    	return json_encode(['error'=>$error]);		
    }
	
	/*
     * 初始化操作
     */
    public function _initialize() 
    {
        //过滤不需要登陆的行为
//      if(in_array(ACTION_NAME,array('login','logout','vertify'))){
//      	//return;
//      }else{
//      	
//      }
//      return json_encode(['error'=>$error]);	

		return json_encode(['error'=>'abcdef']);	
    }
	
	function CheckToken(){
		  if(!isset($_POST['openid']) || !isset($_POST['token'])){
			 //header('HTTP/1.1 880 Param Fail');
			 ErrorSend('-1000');
			 exit();
		 }
		 
		 
		 $token = $_POST['token'];
		 $openid = $_POST['openid'];
		 
		 $redis_ocean = new Redis();
		 $redis_ocean->pconnect(RedISHost, RedISPORT);
		 if(!$redis_ocean->exists($openid)){
			 //没有登陆
			 ErrorSend('-1001');
			 //header('HTTP/1.1 881 Not Login');
			 exit();
		 }
		 $keys = $redis_ocean->get($openid);
		 $rtoken = md5($openid.$keys);
		 //echo '$rtoken='.$rtoken;
		 if($rtoken != $token){
			 //token 失效
			 ErrorSend('-1002');
			 //header('HTTP/1.1 882 Token Fail');
			 exit();
		 }
		 
	}
	
	//返回信息
    protected function returnMsg($code, $msg = '', $data = [])
    {
        $return_data['status'] = $code;
        $return_data['msg'] = $msg;
        $return_data['result'] = $data;
        echo json_encode($return_data);die;
    }
}